#pragma once 

#include <iostream>
#include <string>
#include <unistd.h>

#define SIZE 1024

class Handler{
  public:
    Handler(){}

    void operator()(const std::string& ip, int port, int sockfd)
    {
      char buffer[SIZE];
      while(true){
        ssize_t size = read(sockfd, buffer, sizeof(buffer) - 1);
        if(size < 0){
          std::cerr << "read error" << std::endl;
          break;
        }
        else if(size == 0){
          std::cout << "[" << ip << "]: " << port << " qut..." << std::endl;
          break;
        }
        else{
          buffer[size] = 0;
          std::cout << "[" << ip << "]: " << port << "->" << buffer << std::endl;

          write(sockfd, buffer, size);
        }
      }

      close(sockfd);
    }

    ~Handler(){}
};

class Task{
  private:
    std::string ip;
    int port;
    int sockfd;
    Handler handler;

  public:
    Task(std::string _ip, int _port, int _sockfd):ip(_ip), port(_port), sockfd(_sockfd)
    {}
    Task(){}

    void Run()
    {
      handler(ip, port, sockfd);
    }

    ~Task(){}

};
